package com.sooth.system.menu.mapper;

import com.dream.system.annotation.Mapper;
import com.dream.system.annotation.Param;
import com.dream.system.annotation.Sql;
import com.sooth.system.menu.view.SysMenuDto;
import com.sooth.system.menu.view.SysMenuVo;

import java.util.List;

@Mapper(SysMenuProvider.class)
public interface SysMenuMapper {

    List<SysMenuVo> selectMenuTree(@Param("userId") Long userId, @Param("menu") SysMenuDto menuModel);

    @Sql("""
            SELECT
            	id
            FROM
            	sys_menu
            WHERE
            	parent_id =:parentId
            	AND order_num =:orderNum
                """)
    List<Long> queryByOrderNum(@Param("parentId") Long parentId, @Param("orderNum") Integer orderNum);

    @Sql("""
            UPDATE sys_menu
            SET order_num = order_num + 1
            WHERE
            	parent_id =:parentId
            	AND order_num >=:orderNum
            """)
    Integer updateOrderInc(@Param("parentId") Long parentId, @Param("orderNum") Integer orderNum);


    @Sql("""
            SELECT
            	menu.perm
            FROM
            	( SELECT sys_menu.id, sys_menu.perm FROM sys_menu WHERE perm IS NOT NULL ) menu
            	INNER JOIN sys_role_menu ON menu.id = sys_role_menu.menu_id
            	INNER JOIN sys_user_role ON sys_user_role.role_id = sys_role_menu.role_id
            WHERE
            	sys_user_role.user_id =:userId
            """)
    List<String> queryPermByUserId(@Param("userId") Long userId);
}
